//------------------------------------------------------------------------------ 
//  <copyright file="CaptureFormat.cpp" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
//  </copyright>
//------------------------------------------------------------------------------
#include "..\stdafx.h"
using namespace System::Runtime::InteropServices;

#include "CaptureFormat.h"
#include "RGBCameraEnumerator.h"
#include "SourceReaderCallback.h"
#include "RGBCamera.h"

#include "ks.h"
#include "ksmedia.h"
namespace Microsoft {
    namespace Robotics {
        namespace Vision {
            namespace Cameras {
				namespace Webcam {

/// <summary>
/// Create the Capture Format from the given media type.
/// </summary>
///<param name="pMediaType"> The media type for the capture format</param>
CaptureFormat^ CaptureFormat::FromMediaType(IMFMediaType *pMediaType)
{
    HRESULT hr = S_OK;
    CaptureFormat^ format = gcnew CaptureFormat();
    GUID subType = {0};
    UINT32 rateNum = 0;
    UINT32 rateDenom = 0;
    UINT32 width = 0;
    UINT32 height = 0;

    MF_THROWPTR(pMediaType);

    hr = pMediaType->GetGUID(MF_MT_SUBTYPE, &subType);
    MF_THROWHR(hr);

    format->subType = VideoFormat::FromGuid(FromGUID(subType));

    hr = MFGetAttributeRatio(pMediaType, MF_MT_FRAME_RATE, &rateNum, &rateDenom);
    MF_THROWHR(hr);
    
    format->nFrameRateNumerator = rateNum;
    format->nFrameRateDenominator = rateDenom;
    
    hr = MFGetAttributeSize(pMediaType, MF_MT_FRAME_SIZE, &width, &height);
    MF_THROWHR(hr);

    format->nWidth = width;
    format->nHeight = height;

    return format;
}
}}}}}
